package com.csnf.Servlet;

import com.csnf.entity.Task;
import com.csnf.mapper.TaskMapper;
import com.csnf.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@WebServlet({"/getAllTask","/getTaskByName","/addTask","/deleteTask","/getTaskById","/updateTask"})
public class TaskServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        SqlSession sqlsession = MyBatisUtil.getSqlSessionFactory().openSession(true);
        TaskMapper mapper = sqlsession.getMapper(TaskMapper.class);
        List<Task> allTask = mapper.getAllTask();
        List<Task> taskList = null;
        String servletPath= req.getServletPath();
        switch(servletPath){
            case "/getAllTask":
                taskList = mapper.getAllTask();
                break;
            case "/getTaskByName":
                String taskName = req.getParameter("taskName");
                taskList = mapper.getTaskByName(taskName);
                break;
            case "/deleteTask":
                Integer taskId = Integer.parseInt(req.getParameter("taskId"));
                mapper.deleteTask(taskId);
                resp.sendRedirect("getAllTask");
                return;
            case "/getTaskById":
                Task task = mapper.getTaskById(Integer.parseInt(req.getParameter("taskId")));
                req.setAttribute("task",task);
                req.getRequestDispatcher("taskEdit.jsp").forward(req,resp);
                return;
        }
        req.setAttribute("tasks",taskList);
        req.getRequestDispatcher("taskList.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        SqlSession SqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
        TaskMapper mapper = SqlSession.getMapper(TaskMapper.class);
        String taskName = req.getParameter("taskName");
        String taskGroup = req.getParameter("taskGroup");
        String taskContent = req.getParameter("taskContent");
        String taskExpression = req.getParameter("taskExpression");
        int taskState=Integer.parseInt(req.getParameter("taskState"));
        int taskStrategy = Integer.parseInt(req.getParameter("taskStrategy"));
        int taskCurrent = Integer.parseInt(req.getParameter("taskCurrent"));
        String taskDesc = req.getParameter("taskDesc");

        String taskDateString = req.getParameter("taskDate");
        String taskTimeString = req.getParameter("taskTime");
        LocalDateTime taskDate=null;
        try{
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            taskDate = LocalDateTime.parse(taskDateString+" "+taskTimeString,formatter);
        }catch (DateTimeParseException e){
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Invalid date or time format:" +e.getMessage());
            return;
        }

        Task task = new Task(null,taskName,taskGroup,taskContent,taskExpression,taskState,taskDate,taskStrategy,taskCurrent,taskDesc);
        String servletPath = req.getServletPath();
        switch (servletPath){
            case "/addTask":
                mapper.addTask(task);
                SqlSession.commit();
                break;
            case "/updateTask":
                task.setTaskId(Integer.parseInt(req.getParameter("taskId")));
                mapper.updateTask(task);
                SqlSession.commit();
                break;
        }
       resp.sendRedirect("getAllTask");
    }
}
